package com.ruoyi.spider.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.ruoyi.common.constant.ReturnConstans;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ICallBack;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Guid;
import com.ruoyi.spider.domain.ProcessorConfig;
import com.ruoyi.spider.domain.SpiderArticle;
import com.ruoyi.spider.mapper.SpiderArticleMapper;
import com.ruoyi.spider.spiderConfig.handle.Defaulthandle;
import com.ruoyi.spider.spiderConfig.handle.Imghandle;
import com.ruoyi.spider.spiderConfig.processor.AbstractProcessor;
import com.ruoyi.spider.spiderConfig.processor.DefaultProcessor;
import com.ruoyi.spider.spiderConfig.processor.XiaoDaoPipeline;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.spider.mapper.SpiderTaskMapper;
import com.ruoyi.spider.domain.SpiderTask;
import com.ruoyi.spider.service.ISpiderTaskService;
import com.ruoyi.common.core.text.Convert;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * 爬虫任务Service业务层处理
 * 
 * @author 小光头
 * @date 2021-01-15
 */
@Service
public class SpiderTaskServiceImpl implements ISpiderTaskService {
    @Autowired
    private SpiderTaskMapper spiderTaskMapper;
    @Autowired
    private SpiderArticleMapper spiderArticleMapper;
    /**
     * 查询爬虫任务
     * 
     * @param id 爬虫任务ID
     * @return 爬虫任务
     */
    @Override
    public SpiderTask selectSpiderTaskById(String id)
    {
        return spiderTaskMapper.selectSpiderTaskById(id);
    }

    @Override
    public SpiderTask selectSpiderTaskByTaskId(String taskId) {
        return spiderTaskMapper.selectSpiderTaskByTaskId(taskId);
    }

    /**
     * 查询爬虫任务列表
     * 
     * @param spiderTask 爬虫任务
     * @return 爬虫任务
     */
    @Override
    public List<SpiderTask> selectSpiderTaskList(SpiderTask spiderTask)
    {
        return spiderTaskMapper.selectSpiderTaskList(spiderTask);
    }

    /**
     * 新增爬虫任务
     * 
     * @param spiderTask 爬虫任务
     * @return 结果
     */
    @Override
    public int insertSpiderTask(SpiderTask spiderTask)
    {
        spiderTask.setId(Guid.get());
        spiderTask.setTaskId(Guid.get());
        spiderTask.setCreateTime(DateUtils.getNowDate());
        return spiderTaskMapper.insertSpiderTask(spiderTask);
    }

    /**
     * 修改爬虫任务
     * 
     * @param spiderTask 爬虫任务
     * @return 结果
     */
    @Override
    public int updateSpiderTask(SpiderTask spiderTask)
    {
        return spiderTaskMapper.updateSpiderTask(spiderTask);
    }

    /**
     * 删除爬虫任务对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSpiderTaskByIds(String ids)
    {
        return spiderTaskMapper.deleteSpiderTaskByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除爬虫任务信息
     * 
     * @param id 爬虫任务ID
     * @return 结果
     */
    @Override
    public int deleteSpiderTaskById(String id)
    {
        return spiderTaskMapper.deleteSpiderTaskById(id);
    }

    /**
     *
     * @param taskId
     * @return
     */
    @Override
    public AjaxResult runSpiderTask(String taskId) {
        //查询任务的配置
        SpiderTask spiderTask = this.selectSpiderTaskByTaskId(taskId);
        if(spiderTask!=null){
            spiderTask.getId();

            ProcessorConfig processorConfig = new ProcessorConfig();
            processorConfig.setEntryUrls(spiderTask.getTaskUrl());

            Site site = Site.me().setRetryTimes(3).setTimeOut(3000).setSleepTime(100).addHeader("User-Agent",
                    "ozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.516.400 QQBrowser/9.4.8188.400")
                    .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                    .addHeader("Accept-Encoding", "gzip, deflate, sdch")
                    .addHeader("Upgrade-Insecure-Requests", "1")
                    .addHeader("Referer", "https://www.x6d.com/");


            //执行爬虫逻辑
            AbstractProcessor abstractProcessor = new DefaultProcessor(site,processorConfig,new XiaoDaoPipeline());
            CopyOnWriteArrayList<LinkedHashMap<String, String>> execute = abstractProcessor.execute();
            for (LinkedHashMap<String, String> stringStringLinkedHashMap : execute) {
                //对字符串进行处理
                String spiderDataMap = stringStringLinkedHashMap.get("spiderDataMap");

                //处理字符串
                String spiderArticle1 = new Defaulthandle().process(spiderDataMap).toString();

                //处理图片请求
                String s = new Imghandle().process(spiderArticle1).toString();

                JSONObject jsonObject = JSONObject.parseObject(s);
                Map<String,Object> spiderMap = (Map<String,Object>)jsonObject;

                SpiderArticle spiderArticle = new SpiderArticle();
                spiderArticle.setId(Guid.get());
                spiderArticle.setCreateTime(DateUtils.getNowDate());
                spiderArticle.setContent(spiderMap.get("content").toString());
                spiderArticle.setFristImg(spiderMap.get("fristimg").toString());
                spiderArticle.setTitle(spiderMap.get("title").toString());

                spiderArticleMapper.insertSpiderArticle(spiderArticle);

            }
            return AjaxResult.success();
        }else{
            return AjaxResult.error(ReturnConstans.AJAX_ERROE_MSG);
        }



    }

    public class SpiderCallBack implements ICallBack {
        Map params= Maps.newConcurrentMap();
        @Override
        public void onSuccess() {
            System.out.println(">>>>>>>>>>>>>done>>>>>>>>>>>>>>");
            CopyOnWriteArrayList<LinkedHashMap<String, String>> datas=(CopyOnWriteArrayList<LinkedHashMap<String, String>>)params.get("datas");
            System.out.println(">>>>>>>>>>>>>"+datas.size()+">>>>>>>>>>>>>>");
        }

        @Override
        public void onFail() {

        }

        @Override
        public Map setParams(Map map) {
            params.clear();
            params.putAll(map);
            return params;
        }
    }
}
